package com.wontube.client.extension.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tanggf(2012-5-29)
 *
 */
public class DateUtils {
	private final static String[] longMonths = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	private final static String[] shortMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
	
	
	/**
	 * parse string to Date
	 * @param s 日期格式(MM/dd/yyyy HH:mm:ss), 时间部分可选
	 * @return
	 */
	public static Date parse(String s){
		s = amendTime(s);
		if(s == null){
			return null;
		}
		
		try {
			SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");			
			Date date = format.parse(s);
			return date;
		} catch (ParseException e) {			
			return null;
		}		
	}
	
	public static final Date parse(DateFormat format, String input) {
		try {
			return format.parse(input);
		} catch (ParseException e) {
		}
		
		return null;
	}
	
	/**
	 * September -> 9
	 * @param month
	 * @return
	 */
	public static int getMonth(String month){
		for (int i = 0; i < 12; i++) {
			String m = longMonths[i];
			if(m.equals(month)){
				return i + 1;
			}
		}
		return 0;
	}
	
	
	/**
	 * Sept -> 9
	 * @param shortMonth
	 * @return
	 */
	public static int getShortMonth(String shortMonth){
		for (int i = 0; i < 12; i++) {
			String m = shortMonths[i];
			if(m.equals(shortMonth)){
				return i + 1;
			}
		}
		return 0;
	}
	
	
	
	public static Date getNow(){
		return Calendar.getInstance().getTime();
	}
	
	/**
	 * 修正时间部分
	 * @param s
	 * @return
	 */
	private static String amendTime(String s){
		if(StringUtils.isNullOrEmpty(s)){
			return null;
		}
		String[] dd = s.split("\\s+");
		if(dd.length == 1){
			return s + " 00:00:00";
		}
		
		String[] times = dd[1].split(":");
		if(times.length == 1){
			return s + ":00:00";
		}
		
		if(times.length == 2){
			return s + ":00";
		}
		return dd[0] + " " + dd[1];
	}
	
	/**
	 * eg: 2008
	 * eg: Jul 20, 2007
	 * @param date
	 * @return
	 */
	public static Date toDate(String date){
		if(StringUtils.isNullOrEmpty(date)){
			return null;
		}
		String[] ds = date.split("\\s+");
		if(ds.length == 1){
			SimpleDateFormat format = new SimpleDateFormat("yyyy");
			return DateUtils.parse(format, ds[0]);
		}
		
		int m = DateUtils.getShortMonth(ds[0]);
		String d = ds[1].replace(",", "");
		String dd = ds[2] + "-" + m + "-" + d;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return DateUtils.parse(format, dd);
	}
	
	public static void main(String[] args) {
		String s = "2009-08-10T04:00:07Z";
		s = s.replace("T", "").replace("Z", "");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
		System.err.println(parse(format, s));
	}
}
